package com.rj1101.hscrm.demo.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.rj1101.hscrm.demo.model.Appointmentinfo;
import com.rj1101.hscrm.demo.model.Cinfo;
import com.rj1101.hscrm.demo.model.Emp;
import com.rj1101.hscrm.demo.service.AppointmentService;
import com.rj1101.hscrm.demo.util.Paging;

public class AppointmentAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2539553089627682850L;
	private Appointmentinfo app; 
	private Cinfo cinfo;
	private Emp doctor;
	private Paging pager;
	private List<Appointmentinfo> appList;
	private List<Appointmentinfo> cacheList;
	private AppointmentService appService; 
	private HttpServletRequest request;
	private HttpServletResponse resp; 
	
	public List<Appointmentinfo> getAppList() {
		return appList;
	}
	public void setAppList(List<Appointmentinfo> appList) {
		this.appList = appList;
	}
	public List<Appointmentinfo> getCacheList() {
		return cacheList;
	}
	public Paging getPager() {
		return pager;
	}
	public void setPager(Paging pager) {
		this.pager = pager;
	}
	public Emp getDoctor() {
		return doctor;
	}
	public void setDoctor(Emp doctor) {
		this.doctor = doctor;
	}
	public Cinfo getCinfo() {
		return cinfo;
	}
	public void setCinfo(Cinfo cinfo) {
		this.cinfo = cinfo;
	}
	public Appointmentinfo getApp() {
		return app;
	}
	public void setApp(Appointmentinfo app) {
		this.app = app;
	}
	public AppointmentService getAppService() {
		return appService;
	}
	public void setAppService(AppointmentService appService) {
		this.appService = appService;
	}
	/**
	 *  
	 */
	public String addAppointInfo() throws Exception{
		resp = ServletActionContext.getResponse();
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		if(doctor==null || cinfo==null){
			out.print("{\"statusCode\":\"500\", \"message\":\"该患者或医生不存在\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return ERROR;
		}
		app.setCinfo(cinfo);
		app.setAppointDoctor(doctor.getEmpName());
		if(appService.addAppoitment(app)){
			out.print("{\"statusCode\":\"200\", \"message\":\"恭喜您，添加预约信息成功！\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return SUCCESS;
		}else{
			out.print("{\"statusCode\":\"500\", \"message\":\"添加预约信息失败\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return ERROR;
		}
	}
	/**
	 * 
	 */
	public String alterAppointInfo()throws Exception{
		resp = ServletActionContext.getResponse();
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		app.setCinfo(cinfo);
		app.setAppointDoctor(doctor.getEmpName());
		if(app!=null && appService.alterAppointment(app)){
			out.print("{\"statusCode\":\"200\", \"message\":\"修改预约记录成功！\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return SUCCESS;
		}else{
			out.print("{\"statusCode\":\"500\", \"message\":\"修改失败或未找到该预约记录\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return ERROR;
		}
	}
	/**
	 * 
	 */
	public String delAppointInfo()throws Exception{
		resp = ServletActionContext.getResponse();
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		if(app!=null && appService.delAppointment(app)){
			out.print("{\"statusCode\":\"200\", \"message\":\"恭喜您，删除预约记录成功！\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return SUCCESS;
		}else{
			out.print("{\"statusCode\":\"500\", \"message\":\"删除失败或未找到该预约记录\"," +
					"\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}");
			out.close();
			return ERROR;
		}
	}
	/**
	 * 
	 */
	public String simpleFindAppointInfo() throws Exception{
	
		resp = ServletActionContext.getResponse();
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		app = appService.findByAppointId(app.getAppointId());
		if( null != app){
			appList.removeAll(appList);
			appList.add(app);
			return SUCCESS;
		}else{
			out.print("<p style='margin:10px'><font color=red size=16>很抱歉,未找到预约记录!</font></p>");
			out.close();
			return ERROR;
		}
	}
	/**
	 * 
	 */
	public String findByTimeAppointment()throws Exception{
		
		
		
		return SUCCESS;
	}
	
	/**
	 * 
	 */
	public String findByCelNameAppointment() throws Exception{
		
		List<Appointmentinfo> appDocList = appService.findByDoctor(app.getAppointDoctor());
		if( appDocList.size() > 0 ){
			request.setAttribute("appDocList", appDocList);
			return SUCCESS ;
		}else{
			return ERROR;
		}
		 
	}
	/**
	 * 
	 */
	public String findByCilIDAppointment()throws Exception{
		String customerID = request.getParameter("customerID"); //
		
		int customerId;
		try{
			customerId = Integer.parseInt(customerID);
		}catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}
		List<Appointmentinfo> appList = appService.findByCustomerId(customerId);
		if( appList.size() > 0){
			
			request.setAttribute("appList", appList);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	/**
	 * 
	 */
	public String findByCardIDAppointment() throws Exception{
		
		List<Appointmentinfo> appBycardList = appService.findByCardId(app.getCinfo().getCardId());
		if(appBycardList.size() > 0){
			request.setAttribute("appBycardList", appBycardList);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	/**
	 *  
	 */
	public String findByDocIDAppointment() throws Exception{
		return SUCCESS;
	}	
	
	/**
	 * 
	 */
	public String findByDocNameAppointment() throws Exception{
		List<Appointmentinfo> appDocList = appService.findByDoctor(app.getAppointDoctor());
		if(appDocList.size() > 0){
			request.setAttribute("appDocList", appDocList);
			return SUCCESS;
		}else{
			return ERROR;
		}
	
	}

	public String findAllAppointInfo(){
		appList=  appService.findAllAppInfo();
		cacheList = appList;
		if(null!=appList){
			pager=new Paging();
			pager.setOrderField("appointId");
			pager.setTotalCount(cacheList.size());
			pager.setCurrentPage(1);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	public String pagerAppointInfo(){
		if(cacheList==null){
			cacheList = appService.findAllAppInfo();
		}
		appList = (ArrayList<Appointmentinfo>) appService.pagingAppointInfo("APPOINTMENTINFO", pager);
		pager.setTotalCount(cacheList.size());
		pager.setCurrentPage(pager.getPageNum());
		return SUCCESS;
	}
	
	public String getDetailAppointInfo(){
		int appId = -1;
		try {
			appId = app.getAppointId();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
		
		if(appId>0){
			for(Appointmentinfo appoint : cacheList){
				if(appId == app.getAppointId()){
					app = appoint;
					cinfo = app.getCinfo();
					break;
				}
			}
			return SUCCESS;
		}else{
			return ERROR;
		}
		
	}
}
